package com.joysuch.wwyt.mh.service.impl;

import com.joysuch.wwyt.core.enums.DeleteFlag;
import com.joysuch.wwyt.mh.entity.MhMajorHazardTaskAssign;
import com.joysuch.wwyt.mh.entity.MhMajorHazardTaskRecord;
import com.joysuch.wwyt.mh.entity.MhMajorHazardTaskRecordAssign;
import com.joysuch.wwyt.mh.mapper.MhMajorHazardTaskRecordAssignMapper;
import com.joysuch.wwyt.mh.service.MhMajorHazardTaskRecordAssignService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zqw
 * @since 2022-05-24
 */
@Service
public class MhMajorHazardTaskRecordAssignServiceImpl extends ServiceImpl<MhMajorHazardTaskRecordAssignMapper, MhMajorHazardTaskRecordAssign> implements MhMajorHazardTaskRecordAssignService {

    @Override
    public void saveRecordAssign(Long userId, Long recordId) {
        MhMajorHazardTaskRecordAssign assign = new MhMajorHazardTaskRecordAssign();
        assign.setAssignUser(userId);
        assign.setRecordId(recordId);
        save(assign);
    }

    @Override
    public void batchSaveOverdueRecordAssigns(List<MhMajorHazardTaskRecord> records, Map<Long, Long> recordTaskIdMap, Map<Long, List<MhMajorHazardTaskAssign>> taskAssignMap, Date date) {
        List<MhMajorHazardTaskRecordAssign> recordAssigns = new ArrayList<>();
        records.forEach(record -> {
            Long taskId = recordTaskIdMap.get(record.getId());
            List<MhMajorHazardTaskAssign> taskAssignList = taskAssignMap.get(taskId);
            taskAssignList.forEach(assign -> {
                MhMajorHazardTaskRecordAssign recordAssign = new MhMajorHazardTaskRecordAssign();
                recordAssign.setRecordId(record.getId());
                recordAssign.setAssignUser(assign.getAssignUser());
                recordAssign.setCreateTime(date);
                recordAssign.setUpdateTime(date);
                recordAssign.setOrgCode(assign.getOrgCode());
                recordAssign.setTenentId(assign.getTenentId());
                recordAssign.setDeleteFlag(DeleteFlag.EFFECTIVE.getCode());
                recordAssigns.add(recordAssign);
            });
        });
        saveOrUpdateBatch(recordAssigns);
    }
}
